/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.procedure.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.prod.procedure.dao.model.ProdWorkProcedure;
import com.erp.prod.procedure.dao.model.ProdWorkProcedureCO;
import com.erp.prod.procedure.service.ProdWorkProcedureResourceService;
import com.erp.prod.procedure.service.ProdWorkProcedureService;
import com.erp.prod.workcenter.dao.model.ProdWorkCenter;
import com.erp.prod.workcenter.dao.model.ProdWorkCenterCO;
import com.erp.prod.workcenter.service.ProdWorkCenterService;

@Controller
@RequestMapping("/web/prodWorkProcedure")
public class ProdWorkProcedureWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdWorkProcedureWebController.class);
    
    //服务层注入
    @Autowired
    private ProdWorkProcedureService prodWorkProcedureService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private ProdWorkProcedureResourceService prodWorkProcedureResourceService;
    @Autowired
    private ProdWorkCenterService prodWorkCenterService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdWorkProcedureList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-12-04 18:22:55
     * @author 
     * @param pages
     * @param prodWorkProcedureCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkProcedureList")
    public String getProdWorkProcedureList(Pages pages, ProdWorkProcedureCO prodWorkProcedureCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<ProdWorkProcedure> prodWorkProcedureList = this.prodWorkProcedureService.getDataObjectsForDataAuth("", pages, prodWorkProcedureCO);
        
        //获取工作重心列表
        ProdWorkCenterCO prodWorkCenterCO = new ProdWorkCenterCO();
        prodWorkCenterCO.setStatus("Y");
        prodWorkCenterCO.setApproveStatus("APPROVE");
        List<ProdWorkCenter> prodWorkCenterList = this.prodWorkCenterService.getDataObjects(prodWorkCenterCO);
        
        //循环获取工序的工作重心
        for(ProdWorkProcedure prodWorkProcedure: prodWorkProcedureList) {
            for(ProdWorkCenter prodWorkCenter: prodWorkCenterList) {
                if(prodWorkProcedure.getWorkCenterCode().equals(prodWorkCenter.getWorkCenterCode())) {
                    prodWorkProcedure.setWorkCenterName(prodWorkCenter.getWorkCenterName());
                    break;
                }
            }
        }
        
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("prodWorkProcedureList", prodWorkProcedureList);
        model.addAttribute("pages", pages);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=prodWorkProcedure/prodWorkProcedureList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-12-04 18:22:55
     * @author 
     * @param prodWorkProcedure
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdWorkProcedure")
    public String getProdWorkProcedure(ProdWorkProcedure prodWorkProcedure, Model model) {
        //查询要编辑的数据
        if(prodWorkProcedure!=null&&StringUtils.isNotBlank(prodWorkProcedure.getProcedureCode())) {
            ProdWorkProcedure prodWorkProcedureTemp = this.prodWorkProcedureService.getDataObject(prodWorkProcedure.getProcedureCode());
            if(prodWorkProcedureTemp!=null) {
                prodWorkProcedure = prodWorkProcedureTemp;
            }
        }
        
        //获取工作重心列表
        ProdWorkCenterCO prodWorkCenterCO = new ProdWorkCenterCO();
        prodWorkCenterCO.setStatus("Y");
        prodWorkCenterCO.setApproveStatus("APPROVE");
        List<ProdWorkCenter> prodWorkCenterList = this.prodWorkCenterService.getDataObjects(prodWorkCenterCO);
        
        //审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        
        //页面属性设置
        model.addAttribute("prodWorkProcedure", prodWorkProcedure);
        model.addAttribute("prodWorkCenterList", prodWorkCenterList);
        model.addAttribute("approveStatusMap", approveStatusMap);
        
        return "basic.jsp?content=prodWorkProcedure/prodWorkProcedureEdit";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-12-04 18:22:55
     * @author 
     * @param prodWorkProcedure
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdWorkProcedure")
    public String editProdWorkProcedure(@Valid ProdWorkProcedure prodWorkProcedure, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getProdWorkProcedure";
        }
        
        try {
            //对当前编辑的对象初始化默认的字段
            
            //保存编辑的数据
            this.prodWorkProcedureService.insertOrUpdateDataObject(prodWorkProcedure);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("procedureCode", prodWorkProcedure.getProcedureCode());
        }catch(Exception e){
            if(e.getCause().getClass()==ConstraintViolationException.class) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "工序编码已存在，请重新输入");
                return "redirect:getProdWorkProcedureList";
            }else {
                throw e;
            }
        }
        
        return "redirect:getProdWorkProcedure";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-12-04 18:22:55
     * @author 
     * @param prodWorkProcedure
     * @return String
     *
     */
    @RequestMapping("deleteProdWorkProcedure")
    public String deleteProdWorkProcedure(ProdWorkProcedure prodWorkProcedure, RedirectAttributes attr) {
        //删除数据前验证数据
        if(prodWorkProcedure!=null&&prodWorkProcedure.getProcedureId()!=null&&StringUtils.isNotBlank(prodWorkProcedure.getProcedureCode())) {
            int resourceCount = this.prodWorkProcedureResourceService.getResourceCountByWorkProcedureCode(prodWorkProcedure.getProcedureCode());
            
            if(resourceCount==0) {
                //删除数据
                this.prodWorkProcedureService.deleteDataObject(prodWorkProcedure);
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "工序已关联资源，无法删除");
            }
        }
        
        return "redirect:getProdWorkProcedureList";
    }
    
    
    
    /**
     * 
     * @description 更新审批状态
     * @date 2020年8月4日
     * @author dongbin
     * @param code
     * @param approveStatus
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("updateApproveStatus")
    public String updateApproveStatus(String code, String approveStatus, RedirectAttributes attr) {
        
        if(StringUtils.isNotBlank(code)&&StringUtils.isNotBlank(approveStatus)) {
            //更新审核状态
            this.prodWorkProcedureService.updateApproveStatus(code, approveStatus);
            //提示信息
            attr.addFlashAttribute("hint", "success");
            attr.addAttribute("procedureCode", code);
        }else {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "提交或审批数据错误");
            attr.addAttribute("procedureCode", code);
        }
        
        return "redirect:getProdWorkProcedure";
    }
}
